﻿#include "MyProject.h"

template<> MyProject* CSingleton<MyProject>::msSingleton = 0;

String MyProject::newProject(String body)
{
	if (body.empty()) {
		return "false";
	}
	Json::Value value;
	Json::Reader reader;
	reader.parse(body, value);
	String fileName = value["fileName"].asCString();
	return _myDatabase.create(fileName);
}

String MyProject::openProject(String body)
{
	Json::Value value;
	Json::Reader reader;
	reader.parse(body, value);
	String fileName = value["fileName"].asCString();

	if (!FileUtil::existFile(U2A(fileName)))
	{
		return "false";
	}

	return _myDatabase.open(fileName);
}

void MyProject::closeProject()
{
	_myDatabase.close();
}

String MyProject::getProjectData(String body)
{
	Json::Value value;
	Json::Reader reader;
	reader.parse(body, value);
	String name = value["name"].asCString();
	String type = value["type"].asCString();
	//OCPP_LOG_INFO << U2A("查询:" + type + " " + name) << "\n";
	return _myDatabase.getProjectData(name, type);
}

String MyProject::listTime(String type)
{
	if (type == "GeoObserverData") 
	{
		return _myDatabase.listGeoObserveTime();
	}
	return _myDatabase.listTime();
}

String MyProject::listProjectInTime(String body)
{
	Json::Value value;
	Json::Reader reader;
	reader.parse(body, value);
	String time = value["time"].asCString();
	return _myDatabase.listProjectInTime(time);
}

String MyProject::listProject()
{
	return _myDatabase.listProject();
}

String MyProject::updateProject(String body)
{
	if (body.empty()) {
		return "false";
	}
	OCPP_LOG_INFO << U2A(body) << "\n";
	Json::Value value;
	Json::Reader reader;
	reader.parse(body, value);

	if (value["name"].isNull() || value["desc"].isNull()) {
		return "false";
	}

	String name = value["name"].asCString();
	String desc = ServerUtil::jsonToString(value["desc"]);

	if (!_myDatabase.existProject(name))
	{
		return "fasle";
	}

	return _myDatabase.updateProject(name, desc);
}


String MyProject::createProject(String body)
{
	OCPP_LOG_INFO << U2A(body) << "\n";
	Json::Value value;
	Json::Reader reader;
	reader.parse(body, value);
	String name = value["name"].asCString();
	return _myDatabase.insertProject(name, "");
}

String MyProject::importData1(String body)
{
	//OCPP_LOG_INFO << "importData1\n" << U2A(body) << "\n";

	Json::Value value;
	Json::Reader reader;
	reader.parse(body, value);

	String projectName = value["name"].asCString();
	String projectType = value["projectType"].asCString();
	String time = value["time"].asString();

	bool success = false;

	auto data = value["data"];
	for (const auto& obj : data)
	{
		String id = obj["id"].asString();
		String desc = "";
		if (!obj["desc"].isNull()) {
			Json::Value valueDesc = obj["desc"];
			desc = ServerUtil::jsonToString(valueDesc);
		}
		double x = 0.0, y = 0.0, z = 0.0;
		if (!obj["x"].isNull()) {
			x = std::stod(obj["x"].asString());
		}
		if (!obj["y"].isNull()) {
			y = std::stod(obj["y"].asString());
		}
		if (!obj["z"].isNull()) {
			z = std::stod(obj["z"].asString());
		}
		success = _myDatabase.insert(id, time, x, y, z, projectName, projectType, desc);
		if (!success)
		{
			break;
		}
	}

	return success ? "true" : "false";

}

String MyProject::importData2(String body)
{
	Json::Value value;
	Json::Reader reader;
	reader.parse(body, value);

	String time = value["time"].asString();
	String desc = value["desc"].asCString();

	bool success = _myDatabase.insertGeoObserve(time,desc);

	return success ? "true" : "false";
}

String MyProject::getGeoObserveData(String time)
{
	return _myDatabase.getGeoObserveData(time);
}

String MyProject::updateData1(String body)
{
	OCPP_LOG_INFO << "updateData1\n" << U2A(body) << "\n";

	Json::Value obj;
	Json::Reader reader;
	reader.parse(body, obj);

	String projectName = obj["name"].asCString();
	String projectType = obj["projectType"].asCString();
	String time = obj["time"].asString();

	String id = obj["id"].asString();
	String desc = "";
	if (!obj["desc"].isNull()) {
		Json::Value valueDesc = obj["desc"];
		desc = ServerUtil::jsonToString(valueDesc);
	}
	double x = 0.0, y = 0.0, z = 0.0;
	if (!obj["x"].isNull()) {
		x = std::stod(obj["x"].asString());
	}
	if (!obj["y"].isNull()) {
		y = std::stod(obj["y"].asString());
	}
	if (!obj["z"].isNull()) {
		z = std::stod(obj["z"].asString());
	}
	bool success = _myDatabase.update(id, time, x, y, z, projectName, projectType, desc);
	return success ? "true" : "false";
}

String MyProject::deleteData1(String body)
{
	OCPP_LOG_INFO << "deleteData1\n" << U2A(body) << "\n";

	Json::Value obj;
	Json::Reader reader;
	reader.parse(body, obj);
	String projectName = obj["name"].asCString();
	String projectType = obj["projectType"].asCString();
	String time = obj["time"].asString();
	bool success = _myDatabase.deleteADay(time, projectName, projectType);
	return success ? "true" : "false";
}
